#include "StdAfx.h"
#include "Random.h"

CRandom::CRandom(int key)
{
	int seed;
	if (key == 0)
	{
		__asm
		{
			RDTSC;
			mov seed, eax;
		}
	}
	else seed = key;
	int num2, num3, index;
	_seedArray = new int[0x38];
	num2 = 0x9a4ec86 - abs(seed);
	_seedArray[0x37] = num2;
	num3 = 1;
	for (register int i = 1; i <= 0x36; i++)
	{
		index = (0x15 * i) % 0x37;
		_seedArray[index] = num3;
		num3 = num2 - num3;
		if (num3 < 0) num3 += 0x7fffffff;
		num2 = _seedArray[index];
	}
	for (int j = 1; j <= 4; j++)
	{
		for (register int k = 1; k <= 0x37; k++)
		{
			_seedArray[k] -= _seedArray[1 + ((k + 30) % 0x37)];
			if (_seedArray[k] < 0) _seedArray[k] += 0x7fffffff;
		}
	}
	_inext = 0;
	_inextp = 0x15;
}

CRandom::~CRandom(void)
{
	delete[] _seedArray;
}

int CRandom::Next()
{
	int num;
	_inext++;
	if (_inext >= 0x38) _inext = 1;
	_inextp++;
	if (_inextp >= 0x38) _inextp = 1;
	num = _seedArray[_inext] - _seedArray[_inextp];
	if (num < 0) num += 0x7fffffff;
	_seedArray[_inext] = num;
	return num;
}
